<?php
/**
 * Base Content Block class
 *
 * For block generation you must define Data source class, data source class method,
 * parameters array and block template
 *
 * @category   Epic
 * @package    Epic_View
 * @author     Epic Core Team <rukanhitokiri@gmail.com>
 */
abstract class Epic_View_Abstract
{
	/**
     * Path of file view
     * @var string
     */
    protected $_template;
    
    /**
     * Variable of View object
     * @var array
     */
    protected $_viewVars = array();
    
    /**
     * Initialize set the path to the file view
     *
     * @param string $template
     * @return void
     */
    public function __construct($template = null)
    {
    	if (!is_null($template)) {
			$this->setTemplate($template);
    	}
    	$this->init();
    }
    
    public function init() {}
    
    /**
     * Set the view template file
     *
     * @param string $template
     * @throws Epic_View_Exception if template không tồn tại
     * @return Epic_View_Abstract
     */
    public function setTemplate($template)
    {
        if (!file_exists($template) || !is_readable($template)) {
            throw new Epic_View_Exception('Template: ' . $template . ' does not exist.');  
        }
        $this->_template = $template;
        return $this;
    }
    
    /**
     * Retrive path of file view
     *
     * @return string
     */
    public function getTemplate()
    {
        return $this->_template;
    }
     
    /**
     * Set method magic
     */
    public function __set($name, $value)
    {
        $this->_viewVars[$name] = $value;
    }

    /**
     * Get method magic
     */
    public function __get($name)
    {
        if (isset($this->_viewVars[$name])) {
        	return $this->_viewVars[$name];    
        }
    }
    
    /**
     * Add an object view
     * 
     * @param Epic_View_Abstract $view
     */
    abstract public function addView(Epic_View_Abstract $view);
   
    /**
     * Remove a view object
     * 
     * @param Epic_View_Abstract $view
     */
    abstract public function removeView(Epic_View_Abstract $view);
    
	/**
     * Render view template
     *
     * @throws Epic_View_Exception if not specify path to a file
     * @return string
     */
    public function render()
    {
        if (is_null($this->_template)) {
        	throw new Epic_View_Exception('Not specify path to a file');
        }
        extract($this->_viewVars);
        ob_start();
        include($this->_template);
        return ob_get_clean();
    }
}